<template>
    <div class="article-container">
         <!-- 导航栏 -->
        <van-nav-bar
        class="app-nav-bar"
        title="文章详情"
        left-arrow
        @click-left="$router.push({path:'/'})"
        />
        <!-- 导航栏 -->

        <div class="article-wrap">
            <h1 class="title">{{ article.title }}</h1>
            <van-cell center class="user-info">
              <div slot="title" class="name">{{ article.aut_name }}</div>
              <van-image
                slot="icon"
                class="avatar"
                fit="cover"
                round
                :src="article.aut_photo"
              />
              <div slot="label" class="pubdate">{{ article.pubdate | relativeTime}}</div>
              <van-button
                class="follow-btn"
                :type="article.is_followed ? 'default' : 'info'"
                :icon="article.is_followed ? '' : 'plus'"
                round
                size="small"
                :loading="isFollowLoading"
                @click="onFollow"
            >{{ article.is_followed ? '已关注' : '关注' }}</van-button>
          </van-cell>
          <div
            class="markdown-body"
            v-html="article.content"
            ref="article-content"
          ></div>

          <!-- 文章评论列表 -->
          <van-cell  title="全部评论"></van-cell>
          <comment-list
            :source="articleId"
            :list="commentList"
            @update-total-count="totalCommentCount = $event"
            @reply-click="onReplyClick"
          />
          <!-- /文章评论列表 -->
        </div>
        

        <!-- 底部区域 -->
        <div class="article-bottom">
            <van-button
              class="comment-btn"
              type="default"
              round
              size="small"
              @click="onComment"
            >写评论</van-button>
            <van-icon
              name="comment-o"
              :badge="totalCommentCount"
              color="#777"
            />
            <van-icon
              :color="article.is_collected ? 'orange' : '#777'"
              :name="article.is_collected ? 'star' : 'star-o'"
              @click="onCollect"
            />
            <van-icon
              :color="article.attitude ===1 ? 'hotpink' : '#777'"
              :name="article.attitude === 1 ? 'good-job' : 'good-job-o'"
              @click="onLike"
            />
            <van-icon name="share" color="#777777"></van-icon>
        </div>
        <!-- /底部区域 -->

        <!-- 发布评论 -->
        <van-popup
          v-model="isPostShow"
          position="bottom"
        >
          <post-comment
            :target="articleId"
            @post-success="onPostSuccess"
          />
        </van-popup>
        <!-- /发布评论 -->

        <!-- 评论回复 -->
        <van-popup
          v-model="isReplyShow"
          position="bottom"
        >
        <!-- 这里使用 v-if 的目的是让组件随着弹出层的
        显示进行渲染和销毁,防止加载过的组件不重新渲染
        导致数据不会重新加载的问题 -->
          <comment-reply
            v-if="isReplyShow"
            :comment="replyComment"
            :article-id="articleId"
            @closs="isReplyShow = false"
          />
        </van-popup>
        <!-- /评论回复 -->
    </div>
</template>

<script>
import './github-markdown.css'
import {
      getArticleById,
      addCollect,
      deleteCollect,
      addLike,
      deleteLike
       } from '@/api/article'
import { ImagePreview } from 'vant'
import { addFollow, deleteFollow } from '@/api/user'
import CommentList from './components/comment-list.vue'
import PostComment from './components/post-comment.vue'
import CommentReply from './components/comment-reply.vue'
import store from '@/store/'
import { Dialog } from 'vant'

// 在组件中获取动态路由参数：
//     方式一：this.$route.params.articleId
//     方式二：props 传参，推荐
//         this.articleId
export default {
    name: 'ArticleIndex',
    components: {
        CommentList,
        PostComment,
        CommentReply
    },
    props: {
        articleId: {
            type: [String, Number, Object],
            required: true
        }
    },
    data () {
        return {
            article: {}, //文章数据对象
            isFollowLoading: false, // 关注用户按钮的 loading 状态
            isCollectLoading: false, // 收藏的 loading 状态
            isPostShow: false, // 控制发布评论的显示状态
            commentList: [], // 文章评论列表
            totalCommentCount: 0, // 评论总数据量
            isReplyShow: false, // 控制回复的显示状态
            replyComment: {} // 当前回复评论对象
        }
    },
    computed: {},
    watch: {},
    created () {
        this.loadArticle()
    },
    mounted () {},
    methods: {
        async loadArticle () {
            const { data } = await getArticleById (this.articleId)
            this.article = data.data

            // 数据改变影响视图更新 （DOM数据）不是立即的
            // 所以如果需要在修改数据之后马上操作被该数据影响的视图 DOM，需要把这个代码放到 $this.$nextTick 中
            // this.$nextTick 是 Vue 提供得个方法
            this.$nextTick(() => {
                this.handlePerviewImage()
            })
        },

        handlePerviewImage () {
            // 1.获取文章内容 DOM 容器
            const articleContent = this.$refs['article-content']

            // 2.得到所有的 img 标签
            const imgs = articleContent.querySelectorAll('img')
            
            const imgPaths = [] //收集所有的图片路径

            // 3.循环 img 列表，给 img 注册点击事件
            imgs.forEach((img,index) => {
                imgPaths.push(img.src)
                img.onclick = function () {

                    // 4.在事件处理函数中调用 ImagePrvew() 预览
                    ImagePreview({
                        images: imgPaths, // 预览图片路径列表
                        startPosition: index // 起始位置
                })
                }
            })
        },

        async onFollow () {
            if (store.state.user) {
                if (this.article.is_followed) {
                    this.isFollowLoading = true
                    // 已关注，取消关注
                    await deleteFollow(this.article.aut_id)
                    // this.article.is_followed = false
                } else {
                    // 没有关注，添加关注
                    await addFollow(this.article.aut_id)
                    // this.article.is_followed = true
                }
                this.article.is_followed = !this.article.is_followed
                this.isFollowLoading = false
            } else {
                    Dialog.confirm({
                    title: '访问提示',
                    message: '该功能需要登录才能访问，确认登陆吗'
                })
                .then(() => { // 确认执行这里
                    this.$router.replace({
                        name: 'login',
                        query: {
                            redirect: this.$router.currentRoute.fullPath
                        }
                    })
                })
                .catch(() => { // 取消执行这里
                    // 取消了，中断路由导航
                    return
                })
            }
        },

        async onCollect () {
            this.$toast.loading({
                message: '操作中...',
                forbidClick: true // 禁止背景点击
            })
            if (store.state.user) {
                if (this.article.is_collected) {
                // 已收藏，取消收藏
                await deleteCollect(this.articleId)
                } else {
                    // 没有收藏，添加收藏
                    await addCollect(this.articleId)
                }
                this.article.is_collected = !this.article.is_collected
                this.$toast.success(`${this.article.is_collected ? '' : '取消'}收藏成功`)
            } else {
                    this.$toast({
                        message: '请登录后进行操作'
                    })
                }
            },
            
         async onLike () {
            this.$toast.loading({
                message: '操作中...',
                forbidClick: true // 禁止背景点击
            })
            if (store.state.user) {
                if (this.article.attitude === 1) {
                // 已点赞，取消点赞
                await deleteLike(this.articleId)
                this.article.attitude = -1
                } else {
                    // 没有点赞，添加点赞
                    await addLike(this.articleId)
                    this.article.attitude = 1
                }
                this.$toast.success(`${this.article.attitude === 1 ? '' : '取消'}点赞成功`)
            } else{
                this.$toast({
                        message: '请登录后进行操作'
                    })
            }
        },

        onPostSuccess (comment) {
            // 把发布成功的评论数据对象放到评论列表顶部
            this.commentList.unshift(comment)

            // 更新评论的总数量
            this.totalCommentCount++

            // 关闭发布评论弹出层
            this.isPostShow = false
        },

        onReplyClick (comment) {
            this.replyComment = comment

            // 展示回复内容
            this.isReplyShow = true
        },

        onComment () {
            if (store.state.user) {
                this.isPostShow = true
            } else {
                Dialog.confirm({
                    title: '访问提示',
                    message: '该功能需要登录才能访问，确认登陆吗'
                })
                .then(() => { // 确认执行这里
                    this.$router.replace({
                        name: 'login',
                        query: {
                            redirect: this.$router.currentRoute.fullPath
                        }
                    })
                })
                .catch(() => { // 取消执行这里
                    // 取消了，中断路由导航
                    return
                })
            }
        }
    }
}
</script>

<style scoped>
/deep/.van-icon{
    color: #fff;
}
.title{
    font-size: 20px;
    color: #3a3a3a;
    padding: 14px;
    background-color: #fff;
    margin: 0;
}
.avatar{
    width: 35px;
    height: 35px;
    margin-right: 8px;
}
.name{
    font-size: 12px;
    color: #333333;
}
.pubdate{
    font-size: 12px;
    color: #b4b4b4;
}
.follow-btn{
    width: 85px;
    height: 29px;
}
ul{
    list-style: unset;
}
.markdown-body{
    padding: 14px;
    background-color: #fff;
    list-style: unset;
}
.article-bottom{
    position: fixed;
    flex: 1;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    bottom: 0;
    left: 0;
    right: 0;
    height: 44px;
    background-color: white;
}
.comment-btn {
    width: 141px;
     height: 23px;
    font-size: 15px;
}
.article-wrap{
    position: fixed;
    left: 0;
    right: 0;
    top: 46px;
    bottom: 44px;
    overflow-y: auto;
}
</style>